package com.cloudfun.campusshare.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.lang.Snowflake;

import com.alibaba.fastjson.JSON;
import com.cloudfun.campusshare.common.constant.Code;
import com.cloudfun.campusshare.common.model.dto.BaseHsqReq;

import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.configuration.property.HsqProperty;
import com.cloudfun.campusshare.service.HsqService;
import lombok.Builder;
import org.apache.commons.lang3.CharSet;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.Charset;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Function;


/**
 * @author : zhangyongjie
 * @version : 1.0
 * @createTime : 2021/2/19 13:49
 * @description :
 */
@RestController
@RequestMapping("/hsq")
@Builder
public class HsqController {
    private HsqService hsqService;

    private HsqProperty hsqProperty;


    private static BiFunction<String,String,Boolean> orderInfoVerifier= (weChatOrderNo,verifyCode) ->{
        byte[] decode = Base64.decode(verifyCode);
        String decodeContent = new String(decode, Charset.forName("utf-8"));
        return (weChatOrderNo+"GANDAO").equals(decodeContent);
    };
    @PostMapping(value = "/callback")
    public String callBack(@RequestParam Map<String,Object> map){
        BaseHsqReq baseHsqReq = JSON.parseObject(JSON.toJSONString(map), BaseHsqReq.class);
        return hsqService.handleCallback(baseHsqReq);

    }


    /**
     * 影像资料上传回调
     * @param map
     * @return
     */
    @PostMapping(value = "/imageData/callback")
    public String imageDataCallBack(@RequestParam Map<String,Object> map){
        BaseHsqReq baseHsqReq = JSON.parseObject(JSON.toJSONString(map), BaseHsqReq.class);
        return hsqService.handleCallback(baseHsqReq);

    }

    @GetMapping(value = "/orderInfo/query")
    BaseResult getOrderInfoAfterPay(@RequestParam String weChatOrderNo,@RequestParam String verifyCode){

        Boolean result = orderInfoVerifier.apply(weChatOrderNo, verifyCode);
        if(!result){
            return BaseResultFactory.produceEmptyResult(Code.ERROR,"不是合法请求");
        }
        return BaseResultFactory.produceSuccess(hsqService.queryOrderInfo(weChatOrderNo));
    }
}
